<?php
/**
 * @copyright Copyright (c) 2025 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\Nav as NavList;
use app\admin\model\NavInfo;
use app\admin\validate\NavCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Nav extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['title', 'like', '%' . $param['keywords'] . '%'];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $nav = NavList::where($where)
                ->order('create_time asc')
                ->paginate(['list_rows'=> $rows])
                ->each(function ($item, $key) {
                    $contents = json_decode($item->desc,true);
                    $item->proportion  = $contents[0]?? '';
                    $item->proportion2 = $contents[1]?? '';
                    $item->proportion3 = $contents[2]?? '';
                });;
            return table_assign(0, '', $nav);
        } else {
            return view();
        }
    }
    public function read()
    {
        if (request()->isAjax()) {
            $param = get_params();

            $data['content'] = serialize($param);
            $data['update_time'] = time();
            $data['id'] = $param['id'];
            $res = Db::name('Config')->strict(false)->field(true)->update($data);
            return to_assign();
        } else {
            $conf = Db::name('Config')->where('id', 5)->find();
            View::assign('id', 5);
            if ($conf['content']) {
                $config = unserialize($conf['content']);
            }
            View::assign('config', $config);
            return view();
        }
    }
    //添加
    public function add()
    {
        $param = get_params();

        if (request()->isAjax()) {
            $param['desc'] = json_encode($param['desc']);
            if (!empty($param['id']) && $param['id'] > 0) {
                NavList::where(['id' => $param['id']])
                    ->strict(false)
                    ->field(true)
                    ->update($param);
                add_log('edit', $param['id'], $param);
                return to_assign();
            } else {
                $nid = NavList::strict(false)->field(true)->insertGetId($param);
                add_log('add', $nid, $param);
                return to_assign();
            }
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
			if ($id > 0) {
				$nav = Db::name('Nav')->where(['id' => $id])->find();
                $nav['desc'] = json_decode($nav['desc'],true);
				View::assign('nav', $nav);
			}
			View::assign('id', $id);
			return view();
		}
    }

    //删除
    public function delete()
    {
        $id = get_params('id');
        if (Db::name('Nav')->delete($id) !== false) {
            return to_assign(0, '删除成功');
            add_log('delete', $id, []);
        } else {
            return to_assign(1, '删除失败');
        }
    }


}
